# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='io-console'
pkgname="ruby-${_gemname}"
pkgver=0.5.11
pkgrel=3
pkgdesc='add console capabilities to IO instances'
arch=('loong64' 'x86_64')
url="https://github.com/ruby/${_gemname}"
license=('BSD' 'RUBY')
depends=('ruby')
makedepends=('ruby-rake' 'ruby-rake-compiler')
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('db0bf9ae2e8b8e3b6190d723d1258ccc74936071e33368fbf11a4cda4eca32fac19edf0e5126dfc3a1492b0fee956c0a2fe188014839ff2ed98d6d5cc8bf4297')
b2sums=('e263b54c082f1aca21268ace4eb009747678f5db2eb865b371af7a008e69b5e348e7aba946c7cac949eae4603d632cb1f3ed5fbb3aaf627ff45c868f6930fd2d')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"

  sed --in-place '/SOURCE_DATE_EPOCH/d' rakelib/epoch.rake

  rm --verbose \
    rakelib/changelogs.rake
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"
  local _platform="$(gem env platform | cut -d':' -f2)"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  install --verbose --directory --mode=0755 \
    "tmp_install_default/gemspec/specifications/gems/${_gemname}-${pkgver}" \
    "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "tmp_install${_gemdir}/specifications/default"

  gem build "${_gemname}.gemspec"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  gem install \
    --default \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install_default/gemspec" \
    --bindir "tmp_install_default/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  mv --verbose "tmp_install_default/gemspec/specifications/default/${_gemname}-${pkgver}.gemspec" "tmp_install${_gemdir}/specifications/default/${_gemname}-${pkgver}.gemspec"
  mv --verbose "tmp_install${_gemdir}/gems/${_gemname}-${pkgver}/lib/io" "tmp_install/usr/lib/ruby/${_extension_api_version}/"
  mv --verbose "tmp_install${_gemdir}/extensions/${_platform}/${_extension_api_version}/${_gemname}-${pkgver}/io" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}/"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install${_gemdir}/cache/" \
    "tmp_install${_gemdir}/build_info/" \
    "tmp_install${_gemdir}/extensions/" \
    "tmp_install${_gemdir}/gems/" \
    "tmp_install${_gemdir}/plugins/" \
    "tmp_install${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" \
    "tmp_install${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/"
}

check() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake test
}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install --verbose -D --mode=0644 README.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}

# vim: tabstop=2 shiftwidth=2 expandtab:
